<?php
/**
 * 后台Index相关
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Exception;

/**
 * 文章内容管理
 */
class SettingController extends CommonController {

    public function index(){
        $imgplay = D("Imgplay")->select();

        $this->assign('imgplay',$imgplay);
        $this->display();
    }

    public function add(){
        if($_POST) {
            if (!isset($_POST['img']) || !$_POST['img']) {
                return show(0, '请上传图片');
            }
            if (!isset($_POST['url']) || !$_POST['url']) {
                return show(0, '请填写URL');
            }
            if ($_POST['id']) {
                return $this->save($_POST);
            }
            $cid = D("Imgplay")->insert($_POST);
            if ($cid) {
                return show(1, '新增成功');
            }
            return show(0, '新增失败');
        }else{;
            $this->display();
        }
    }

    public function edit(){
        $id = $_GET['id'];
        $imgplay = D("Imgplay")->find($id);
        $this->assign('imgplay',$imgplay);
        $this->display();
    }

    public function setting() {
        if($_POST['id']){
            return $this->saveset($_POST);
        }
        $id=1;
        $set = D("Setting")->find($id);
        $this->assign('set',$set);
        $this->display();
    }

    public function saveset($data){
        $id = $data['id'];
        unset($data['id']);
        try {
            $id = D("Setting")->updateById($id, $data);
            if($id === false) {
                return show(0,'更新失败');
            }
            return show(1,'更新成功');
        }catch(Exception $e) {
            return show(0,$e->getMessage());
        }
    }

    public function save($data){
        $id = $data['id'];
        unset($data['id']);
        try {
            $id = D("Imgplay")->updateimgById($id, $data);
            if($id === false) {
                return show(0,'更新失败');
            }
            return show(1,'更新成功');
        }catch(Exception $e) {
            return show(0,$e->getMessage());
        }
    }

    public function setStatus(){
        try {
            if ($_POST) {
                $id = $_POST['id'];
                $status = $_POST['status'];
                if (!$id) {
                    return show(0, 'ID不存在');
                }
                $res = D("Imgplay")->updateById($id, $status);
                if ($res) {
                    return show(1, '操作成功');
                } else {
                    return show(0, '操作失败');
                }
            }
            return show(0, '没有提交的内容');
        }catch(Exception $e) {
            return show(0, $e->getMessage());
        }
    }

}